package com.lemon.rpcframe.dao.behavior;

import java.util.List;

import com.lemon.rpcframe.annotation.ServiceBehavior;
import com.lemon.rpcframe.dao.DBHelper;
import com.lemon.rpcframe.dao.contract.INodeGroupService;
import com.lemon.rpcframe.dao.entity.NodeGroupBean;
import com.lemon.rpcframe.util.StringUtils;

@ServiceBehavior
public class NodeGroupServiceImpl implements INodeGroupService {

    public static final int DEFAULT_PAGESIZE = 30;

    @Override
    public long insert(NodeGroupBean entity) throws Exception {
        if (null == entity) {
            return 0l;
        }
        long id = 0l;
        Object obj = DBHelper.getInstance().getdaoHelper().sql.insert(entity);
        if (null != obj) {
            id = (long) obj;
        }
        return id;
    }

    @Override
    public long delete(String nodetype, String name) throws Exception {
        if (StringUtils.isNullOrEmpty(nodetype) || StringUtils.isNullOrEmpty(name)) {
            return -1l;
        }

        String where = String.format(" nodetype='%s' and name ='%s' ", nodetype, name);
        DBHelper.getInstance().getdaoHelper().sql.deleteByCustom(NodeGroupBean.class, where);
        return 0;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<NodeGroupBean> loadByType(String nodetype) throws Exception {
        if (StringUtils.isNullOrEmpty(nodetype)) {
            return null;
        }

        String where = String.format(" nodetype='%s' ", nodetype);
        List<NodeGroupBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByCustom(NodeGroupBean.class, "", where, "");
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public NodeGroupBean loadByTypeName(String nodetype, String name) throws Exception {
        if (StringUtils.isNullOrEmpty(nodetype) || StringUtils.isNullOrEmpty(name)) {
            return null;
        }
        String where = String.format(" nodetype='%s' and name ='%s' limit 1", nodetype, name);
        List<NodeGroupBean> list = DBHelper.getInstance().getdaoHelper().sql.getListByCustom(NodeGroupBean.class, "", where, "");

        if (list != null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
}
